//
//  TemplateFactory.m
//  ResourceLoader
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TemplateFactory.h"
#import "JSONKit.h"
#import "ResourceLoader.h"

@implementation TemplateFactory


static TemplateFactory * _instance;

+(TemplateFactory*)getInstance
{
    @synchronized(self)
    {
        if(_instance == nil)
            _instance = [TemplateFactory new];
    }
    return _instance;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        
    }
    return self;
}



-(TemplateView*)createViewByTemplateFileName:(NSString*)name withModuleKey:(NSString*)moduleKey withAutoID:(NSString*)autoID
{
    NSString * rootpath = [[ResourceLoader getInstance]getModulePathByKey:moduleKey withAutoID:autoID];
    NSString * filePath = [NSString stringWithFormat:@"%@/%@",rootpath,name];
    
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary * templateDic = [data objectFromJSONData];
    
    NSString * targetTemplate = [templateDic objectForKey:@"template"];
   
    Class targetClass = NSClassFromString(targetTemplate);
    
    TemplateView * view = [[targetClass alloc]initWithTemplate:templateDic];
//    view.autoID = autoID;
//    view.moduleKey = moduleKey;
    view.rootPath = rootpath;
    
    return view;
}

-(TemplateView*)createViewByTemplateFileName:(NSString*)name RootPath:(NSString *)rootpath
{
    NSString * filePath = [NSString stringWithFormat:@"%@/%@",rootpath,name];
    
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary * templateDic = [data objectFromJSONData];
    
    NSString * targetTemplate = [templateDic objectForKey:@"template"];
    
    Class targetClass = NSClassFromString(targetTemplate);
    
    TemplateView * view = [[targetClass alloc]initWithTemplate:templateDic];
    view.rootPath = rootpath;
    
    return view;
}

@end
